﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class EventTriggerListener : EventTrigger
{
	public delegate void VoidDelegate(GameObject go, BaseEventData eventData);
    
	public VoidDelegate onClick;
	public VoidDelegate onPointClick;		//一个点的点击, 无视Button按钮面积大小。   防止Drag 时响应Click
	public VoidDelegate onDown;
    public VoidDelegate onUp;
    public VoidDelegate onEnter;
	public VoidDelegate onExit;
    public VoidDelegate onMove;
	public VoidDelegate onBeginDrag;
	public VoidDelegate onDrag;
	public VoidDelegate onEndDrag;
    public VoidDelegate onDrop;
    public VoidDelegate onSelect;
    public VoidDelegate onDeselect;
    public VoidDelegate onUpdateSelect;

	private Vector2 _clickPoint;

    public static EventTriggerListener Get(GameObject go)
	{
		EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
		if (listener == null) listener = go.AddComponent<EventTriggerListener>();
		return listener;
	}

    public static EventTriggerListener Get(MonoBehaviour component)
	{
		return Get(component.gameObject);
	}

    public override void OnPointerClick(PointerEventData eventData)
	{
		if (onClick != null) onClick(gameObject, eventData);
	}

	public override void OnPointerDown(PointerEventData eventData)
	{
		_clickPoint = eventData.position;
		if (onDown != null) onDown(gameObject, eventData);
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
		if ( _clickPoint == eventData.position && onPointClick != null) {
			onPointClick (gameObject, eventData);
		}
        if (onUp != null) onUp(gameObject, eventData);
    }

    public override void OnPointerEnter(PointerEventData eventData)
	{
		if (onEnter != null) onEnter(gameObject, eventData);
	}

	public override void OnPointerExit(PointerEventData eventData)
	{
		if (onExit != null) onExit(gameObject, eventData);
    }

    public override void OnMove(AxisEventData eventData)
    {
        if (onMove != null) onMove(gameObject, eventData);
    }

	public override void OnBeginDrag(PointerEventData eventData)
	{
		if (onBeginDrag != null) onBeginDrag(gameObject, eventData);
	}

    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject, eventData);
    }

	public override void OnEndDrag(PointerEventData eventData)
	{
		if (onEndDrag != null) onEndDrag(gameObject, eventData);
	}

    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(gameObject, eventData);
    }

    public override void OnSelect(BaseEventData eventData)
	{
		if (onSelect != null) onSelect(gameObject, eventData);
    }

    public override void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(gameObject, eventData);
    }

    public override void OnUpdateSelected(BaseEventData eventData)
	{
		if (onUpdateSelect != null) onUpdateSelect(gameObject, eventData);
	}

    // --------------------------------------------------------------------------------------------
    // 扩展接口
    public static void AddClick(GameObject go, VoidDelegate onClick)
    {
        Get(go).onClick += onClick;
    }

	public static void AddClick(Component component, VoidDelegate onClick)
    {
        AddClick(component.gameObject, onClick);
    }

    public static void RemoveClick(GameObject go, VoidDelegate onClick)
    {
        Get(go).onClick -= onClick;
    }

    public static void RemoveClick(MonoBehaviour component, VoidDelegate onClick)
    {
        RemoveClick(component.gameObject, onClick);
    }

    public static void ClearClick(GameObject go)
    {
        Get (go).onClick = null;
    }

    public static void ClearClick(MonoBehaviour component)
    {
        ClearClick (component.gameObject);
    }


    public static void AddDown(GameObject go, VoidDelegate onDown)
    {
        Get(go).onDown += onDown;
    }

    public static void AddDown(MonoBehaviour component, VoidDelegate onDown)
    {
        AddDown(component.gameObject, onDown);
    }

    public static void RemoveDown(GameObject go, VoidDelegate onDown)
    {
        Get(go).onDown -= onDown;
    }

    public static void RemoveDown(MonoBehaviour component, VoidDelegate onDown)
    {
        RemoveDown(component.gameObject, onDown);
    }

    public static void ClearDown(GameObject go)
    {
        Get (go).onDown = null;
    }

    public static void ClearDown(MonoBehaviour component)
    {
        ClearDown (component.gameObject);
    }


    public static void AddUp(GameObject go, VoidDelegate onUp)
    {
        Get(go).onUp += onUp;
    }

    public static void AddUp(MonoBehaviour component, VoidDelegate onUp)
    {
        AddUp(component.gameObject, onUp);
    }

    public static void RemoveUp(GameObject go, VoidDelegate onUp)
    {
        Get(go).onUp -= onUp;
    }

    public static void RemoveUp(MonoBehaviour component, VoidDelegate onUp)
    {
        RemoveUp(component.gameObject, onUp);
    }

    public static void ClearUp(GameObject go)
    {
        Get (go).onUp = null;
    }

    public static void ClearUp(MonoBehaviour component)
    {
        ClearUp (component.gameObject);
    }


    public static void AddEnter(GameObject go, VoidDelegate onEnter)
    {
        Get(go).onEnter += onEnter;
    }

    public static void AddEnter(MonoBehaviour component, VoidDelegate onEnter)
    {
        AddEnter(component.gameObject, onEnter);
    }

    public static void RemoveEnter(GameObject go, VoidDelegate onEnter)
    {
        Get(go).onEnter -= onEnter;
    }

    public static void RemoveEnter(MonoBehaviour component, VoidDelegate onEnter)
    {
        RemoveEnter(component.gameObject, onEnter);
    }

    public static void ClearEnter(GameObject go)
    {
        Get (go).onEnter = null;
    }

    public static void ClearEnter(MonoBehaviour component)
    {
        ClearEnter (component.gameObject);
    }


    public static void AddExit(GameObject go, VoidDelegate onExit)
    {
        Get(go).onExit += onExit;
    }

    public static void AddExit(MonoBehaviour component, VoidDelegate onExit)
    {
        AddExit(component.gameObject, onExit);
    }

    public static void RemoveExit(GameObject go, VoidDelegate onExit)
    {
        Get(go).onExit -= onExit;
    }

    public static void RemoveExit(MonoBehaviour component, VoidDelegate onExit)
    {
        RemoveExit(component.gameObject, onExit);
    }

    public static void ClearExit(GameObject go)
    {
        Get (go).onExit = null;
    }

    public static void ClearExit(MonoBehaviour component)
    {
        ClearExit (component.gameObject);
    }


    public static void AddMove(GameObject go, VoidDelegate onMove)
    {
        Get(go).onMove += onMove;
    }

    public static void AddMove(MonoBehaviour component, VoidDelegate onMove)
    {
        AddMove(component.gameObject, onMove);
    }

    public static void RemoveMove(GameObject go, VoidDelegate onMove)
    {
        Get(go).onMove -= onMove;
    }

    public static void RemoveMove(MonoBehaviour component, VoidDelegate onMove)
    {
        RemoveMove(component.gameObject, onMove);
    }

    public static void ClearMove(GameObject go)
    {
        Get (go).onMove = null;
    }

    public static void ClearMove(MonoBehaviour component)
    {
        ClearMove (component.gameObject);
    }


	public static void AddBeginDrag(GameObject go, VoidDelegate onBeginDrag)
	{
		Get (go).onBeginDrag += onBeginDrag;
	}

	public static void AddBeginDrag(MonoBehaviour component, VoidDelegate onBeginDrag)
	{
		AddBeginDrag(component.gameObject, onBeginDrag);
	}

	public static void RemoveBeginDrag(GameObject go, VoidDelegate onBeginDrag)
	{
		Get(go).onBeginDrag -= onBeginDrag;
	}

	public static void RemoveBeginDrag(MonoBehaviour component, VoidDelegate onBeginDrag)
	{
		RemoveDrag(component.gameObject, onBeginDrag);
    }

    public static void ClearBeginDrag(GameObject go)
    {
        Get (go).onBeginDrag = null;
    }

    public static void ClearBeginDrag(MonoBehaviour component)
    {
        ClearBeginDrag (component.gameObject);
    }


    public static void AddDrag(GameObject go, VoidDelegate onDrag)
    {
        Get(go).onDrag += onDrag;
    }

    public static void AddDrag(MonoBehaviour component, VoidDelegate onDrag)
    {
        AddDrag(component.gameObject, onDrag);
    }

	public static void RemoveDrag(GameObject go, VoidDelegate onDrag)
	{
		Get(go).onDrag -= onDrag;
	}

	public static void RemoveDrag(MonoBehaviour component, VoidDelegate onDrag)
	{
		RemoveDrag(component.gameObject, onDrag);
    }

    public static void ClearDrag(GameObject go)
    {
        Get (go).onDrag = null;
    }

    public static void ClearDrag(MonoBehaviour component)
    {
        ClearDrag (component.gameObject);
    }


	public static void AddEndDrag(GameObject go, VoidDelegate onEndDrag)
	{
		Get (go).onEndDrag += onEndDrag;
	}

	public static void AddEndDrag(MonoBehaviour component, VoidDelegate onEndDrag)
	{
		AddEndDrag(component.gameObject, onEndDrag);
	}

	public static void RemoveEndDrag(GameObject go, VoidDelegate onEndDrag)
	{
		Get(go).onEndDrag -= onEndDrag;
	}

	public static void RemoveEndDrag(MonoBehaviour component, VoidDelegate onEndDrag)
	{
		RemoveDrag(component.gameObject, onEndDrag);
    }

    public static void ClearEndDrag(GameObject go)
    {
        Get (go).onEndDrag = null;
    }

    public static void ClearEndDrag(MonoBehaviour component)
    {
        ClearEndDrag (component.gameObject);
    }


    public static void AddDrop(GameObject go, VoidDelegate onDrop)
    {
        Get(go).onDrop += onDrop;
    }

    public static void AddDrop(MonoBehaviour component, VoidDelegate onDrop)
    {
        AddDrop(component.gameObject, onDrop);
    }

    public static void RemoveDrop(GameObject go, VoidDelegate onDrop)
    {
        Get(go).onDrop -= onDrop;
    }

    public static void RemoveDrop(MonoBehaviour component, VoidDelegate onDrop)
    {
        RemoveDrop(component.gameObject, onDrop);
    }

    public static void ClearDrop(GameObject go)
    {
        Get (go).onDrop = null;
    }

    public static void ClearDrop(MonoBehaviour component)
    {
        ClearDrop (component.gameObject);
    }


    public static void AddSelect(GameObject go, VoidDelegate onSelect)
    {
        Get(go).onSelect += onSelect;
    }

    public static void AddSelect(MonoBehaviour component, VoidDelegate onSelect)
    {
        AddSelect(component.gameObject, onSelect);
    }

    public static void RemoveSelect(GameObject go, VoidDelegate onSelect)
    {
        Get(go).onSelect -= onSelect;
    }

    public static void RemoveSelect(MonoBehaviour component, VoidDelegate onSelect)
    {
        RemoveSelect(component.gameObject, onSelect);
    }

    public static void ClearSelect(GameObject go)
    {
        Get (go).onSelect = null;
    }

    public static void ClearSelect(MonoBehaviour component)
    {
        ClearSelect (component.gameObject);
    }


    public static void AddDeselect(GameObject go, VoidDelegate onDeselect)
    {
        Get(go).onDeselect += onDeselect;
    }

    public static void AddDeselect(MonoBehaviour component, VoidDelegate onDeselect)
    {
        AddDeselect(component.gameObject, onDeselect);
    }

    public static void RemoveDeselect(GameObject go, VoidDelegate onDeselect)
    {
        Get(go).onDeselect -= onDeselect;
    }

    public static void RemoveDeselect(MonoBehaviour component, VoidDelegate onDeselect)
    {
        RemoveDeselect(component.gameObject, onDeselect);
    }

    public static void ClearDeselect(GameObject go)
    {
        Get (go).onDeselect = null;
    }

    public static void ClearDeselect(MonoBehaviour component)
    {
        ClearDeselect (component.gameObject);
    }


    public static void AddUpdateSelect(GameObject go, VoidDelegate onUpdateSelect)
    {
        Get(go).onUpdateSelect += onUpdateSelect;
    }

    public static void AddUpdateSelect(MonoBehaviour component, VoidDelegate onUpdateSelect)
    {
        AddUpdateSelect(component.gameObject, onUpdateSelect);
    }

    public static void RemoveUpdateSelect(GameObject go, VoidDelegate onUpdateSelect)
    {
        Get(go).onUpdateSelect -= onUpdateSelect;
    }

    public static void RemoveUpdateSelect(MonoBehaviour component, VoidDelegate onUpdateSelect)
    {
        RemoveUpdateSelect(component.gameObject, onUpdateSelect);
    }

    public static void ClearUpdateSelect(GameObject go)
    {
        Get (go).onUpdateSelect = null;
    }

    public static void ClearUpdateSelect(MonoBehaviour component)
    {
        ClearUpdateSelect (component.gameObject);
    }


	// 特殊点击
	public static void AddPointClick(GameObject go, VoidDelegate onPointClick)
	{
		Get(go).onPointClick += onPointClick;
	}

	public static void AddPointClick(MonoBehaviour component, VoidDelegate onPointClick)
	{
		AddClick(component.gameObject, onPointClick);
	}

	public static void RemovePointClick(GameObject go, VoidDelegate onPointClick)
	{
		Get(go).onPointClick -= onPointClick;
	}

	public static void RemovePointClick(MonoBehaviour component, VoidDelegate onPointClick)
	{
		RemoveClick(component.gameObject, onPointClick);
    }

    public static void ClearPointClick(GameObject go)
    {
        Get (go).onPointClick = null;
    }

    public static void ClearPointClick(MonoBehaviour component)
    {
        ClearPointClick (component.gameObject);
    }
}